layui.use(["layer", "laydate", "laypage"], function () {
    const layer = layui.layer;
    const laydate = layui.laydate;
    const laypage = layui.laypage;

    /** *************************************************************** */
    if ($("#menuText").val().trim() === "") {
        $("#menuBtn1").attr("disabled", true);
    }
    $("#menuText").on("keydown", function (event) {
        if (event.keyCode === 13)
            $("#menuBtn2").click();
    });
    $("#menuBtn1").on("click", function () {
        window.location.href = "showlist";
    });
    $("#menuBtn2").on("click", function () {
        const name = $("#menuText").val();
        if (name.trim() !== "")
            window.location.href = "showlist?name=" + name;
    });
    /** *************************************************************** */
    /** 初始化表格 */
    const name = $("#menuText").val();
    $("#tab1 tbody tr").each(function () {
        const id = $(this).attr("id");
        /** *********************************************************** */
        if (name.trim() !== "") {
            const text = $(this).find("td:eq(1) a").text();
            const font = "<span>" + name + "</span>";
            const expr = new RegExp(name, "gm");
            const cont = text.replace(expr, font);
            $(this).find("td:eq(1) a").html(cont);
        }
        /** *********************************************************** */
        $(this).find("input[type=button]").click(function () {
            window.open("updateview?id=" + id);
        });
    });
    /** *************************************************************** */

    $("#export").on("click", function () {
        layer.open({
            type: 1,
            title: "数据统计",
            area: ["500px", "375px"],
            content: $("#comtext").html()
        });
        laydate.render({
            elem: ".layui-layer-content input[name=date1]"
        });
        laydate.render({
            elem: ".layui-layer-content input[name=date2]"
        });
    });
    $("body").on("click", ".layui-layer-content input[value=导出]", function () {
        const date1 = $(".layui-layer-content input[name=date1]").val();
        const date2 = $(".layui-layer-content input[name=date2]").val();
        const scope = [];
        $(".layui-layer-content input[name=scope]:checked").each(function () {
            scope.push($(this).val());
        });
        const index = $(".layui-layer-content input[name=index]:checked").val();
        if (date1 == null || date1 === "") {
            layer.msg("请选择开始日期！", {icon: 2});
            return false;
        }
        if (date2 == null || date2 === "") {
            layer.msg("请选择结束日期！", {icon: 2});
            return false;
        }
        if (date1 > date2) {
            layer.msg("开始日期大于结束日期！", {icon: 2});
            return false;
        }
        if (scope.length === 0) {
            layer.msg("请选择统计范围！", {icon: 2});
            return false;
        }
        window.open("export?date1=" + date1 + "&date2=" + date2 + "&scope=" + scope + "&index=" + index);
    });

    /** *************************************************************** */
    laypage.render({
        elem: "page",
        curr: $("#page").data("p1"),
        count: $("#page").data("p2"),
        limit: 15,
    });
    $("#page a").on("click", function () {
        let page = $(this).text();
        if (page === "上一页")
            page = Number($("#page").data("p1") - 1);
        if (page === "下一页")
            page = Number($("#page").data("p1") + 1);
        location.href = "showlist?name=" + name + "&page=" + page;
    });
    $(".layui-disabled").off("click");

});
